﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using System.Collections.Generic;
using System.IO;

namespace dnp.Framework.Web
{
    public class WebLogFile : IEnumerable<LogEntry>
    {
        private string _fileName;
        private IWebLogFileProvider _provider;

        public WebLogFile(string fileName, IWebLogFileProvider provider)
        {
            _fileName = fileName;
            _provider = provider;
        }

        public IEnumerator<LogEntry> GetEnumerator()
        {
            using (StreamReader reader =
                new StreamReader(this.FileName))
            {
                string line;
                while ((line = reader.ReadLine())
                     != null)
                {
                    // The parser may want to skip some entries
                    LogEntry result = this.Provider.Parse(line);
                    if (result != null)
                        yield return result;
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public IWebLogFileProvider Provider
        {
            get { return _provider; }
        }

    }
}
